package activity.Left;

import android.annotation.TargetApi;
import android.app.AppOpsManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.hichip.campro.R;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import base.HiActivity;
import butterknife.BindView;
import common.TitleView;

public class LeftMoreActivty extends HiActivity implements View.OnClickListener {
    @BindView(R.id.title) TitleView title;
    @BindView(R.id.tv_isopen) TextView tv_isopen;
    @BindView(R.id.rl_notification) RelativeLayout rl_notification;
    @BindView(R.id.iv_arrow) ImageView iv_arrow;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_left_more;
    }

    @TargetApi(Build.VERSION_CODES.KITKAT)
    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        initViewAndData();
        if (isNotificationEnable(this)) {
            tv_isopen.setText(getString(R.string.opended));
            iv_arrow.setVisibility(View.INVISIBLE);
        } else {
            tv_isopen.setText(getString(R.string.shut_down));
        }
        setListeners();
    }

    private void setListeners() {
        rl_notification.setOnClickListener(this);
    }

    private void initViewAndData() {
        title.setButton(TitleView.NAVIGATION_BUTTON_LEFT);
        title.setTitle(getString(R.string.more));
        title.setNavigationBarButtonListener(new TitleView.NavigationBarButtonListener() {
            @Override
            public void OnNavigationButtonClick(int which) {
                switch (which) {
                    case TitleView.NAVIGATION_BUTTON_LEFT:
                        finish();
                        break;
                }
            }
        });
    }

    /*
     * 判断通知权限是否打开
     */
    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    private boolean isNotificationEnable(Context context) {
        AppOpsManager mAppOps = (AppOpsManager) context.getSystemService(APP_OPS_SERVICE);
        ApplicationInfo appInfo = context.getApplicationInfo();

        String pkg = context.getApplicationContext().getPackageName();
        int uid = appInfo.uid;

        Class appOpsClass = null; /* Context.APP_OPS_MANAGER */

        try {
            appOpsClass = Class.forName(AppOpsManager.class.getName());
            Method checkOpNoThrowMethod = appOpsClass.getMethod("checkOpNoThrow", Integer.TYPE, Integer.TYPE, String.class);

            Field opPostNotificationValue = appOpsClass.getDeclaredField("OP_POST_NOTIFICATION");
            int value = (int) opPostNotificationValue.get(Integer.class);
            return ((int) checkOpNoThrowMethod.invoke(mAppOps, value, uid, pkg) == AppOpsManager.MODE_ALLOWED);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_notification:
                if (iv_arrow.getVisibility() == View.VISIBLE) {
                    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        Intent intent = new Intent();
                        intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
                        intent.putExtra("app_package", getPackageName());
                        intent.putExtra("app_uid", getApplicationInfo().uid);
                        startActivity(intent);
                        finish();
                    } else if (android.os.Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
                        Intent intent = new Intent();
                        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                        intent.addCategory(Intent.CATEGORY_DEFAULT);
                        intent.setData(Uri.parse("package:" + getPackageName()));
                        startActivity(intent);
                        finish();
                    }
                }
                break;
        }
    }
}
